package cn.edu.hqu.cst.app;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.graphics.drawable.AnimationDrawable;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.widget.Button;
import android.widget.FrameLayout;
import android.widget.ImageView;

import java.util.Timer;
import java.util.TimerTask;

public class UC3 extends AppCompatActivity {
    private FrameLayout frameLayout=null;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_uc3);
        Button button15 = (Button) findViewById(R.id.button15);
        button15.setOnClickListener(new View.OnClickListener(){
            @Override
            public void onClick(View v){

                Intent i = new Intent(UC3.this , MainActivity.class);

                startActivity(i);
            }
        });
        frameLayout=(FrameLayout) findViewById(R.id.frame);
        //使用计时器
        Timer timer=new Timer();
        timer.scheduleAtFixedRate(new TimerTask() {
            int i=0;
            @Override
            public void run() {
                i++;
                System.out.println(i);
                Message msg=new Message();
                msg.what=i;
                i%=5;
                //发送消息
                handler.sendMessage(msg);
            }
        }, 1000, 1000);
    }
    //使用Handler实现UI线程与Timer线程之间的通信
    Handler handler=new Handler(){
        //处理消息更新UI线程
        @Override
        public void handleMessage(Message msg) {
            switch(msg.what){

                case 1:frameLayout.setForeground(getResources().getDrawable(R.drawable.a));;break;
                case 2:frameLayout.setForeground(getResources().getDrawable(R.drawable.b));break;
                case 3:frameLayout.setForeground(getResources().getDrawable(R.drawable.c));break;
                case 4:frameLayout.setForeground(getResources().getDrawable(R.drawable.d));break;
                case 5:frameLayout.setForeground(getResources().getDrawable(R.drawable.e));break;
                default:System.out.println("哈哈,没有图片");break;
            }
        }

    };
}




